import requests
from lxml import etree
import db
import re
from tqdm import tqdm
import time
import json

def saveTxt(url):
    res = requests.get(url)
    res.encoding = 'utf-8'
    tree = etree.HTML(res.text)
    title = tree.xpath('//div[@class="bookname"]/h1/text()')[0]
    content = tree.xpath('//div[@id="content"]/text()')
    title = title + ".txt"
    with open(title,'w',encoding='utf-8') as f:
        for i in tqdm(content):
            f.write(i)
    print(title+"下载完成")

def saveMysql(url):
    res = requests.get(url)
    res.encoding = 'utf-8'
    tree = etree.HTML(res.text)
    title = tree.xpath('//div[@class="bookname"]/h1/text()')[0]
    content = tree.xpath('//div[@id="content"]/text()')
    print(content)

    sql = "INSERT INTO `gohub_user_test` (`name`, `age`) VALUES (%s, %s)"
    cleaned_list = [item for item in content if item != '\xa0\xa0\xa0\xa0']

    content = json.dumps(content)
    data = [
        (title, content),
    ]
    #list = db.insrtall(sql, data)


if __name__ == '__main__':
    url = 'http://www.xbiqugu.net/122/122193/45817433.html'
    #saveTxt(url)
    saveMysql(url)


    #list = db.insrtall(sql, data)


    #sql = 'SELECT * FROM gohub_user_test'
    # sql = "INSERT INTO `gohub_user_test` (`name`, `age`) VALUES (%s, %s)"
    # #list = db.select(sql)
    # data = [
    #     ('张三','20'),
    #     ('李四','25'),
    # ]
    # list = db.insrtall(sql,data)

    #进度条
    #pip install tqdm
    #from tqdm import tqdm  引入
    #with tqdm(total=len(list), file=f, desc="Writing") as progress_bar:
    #progress_bar.update()  # 更新进度条
    #
